﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_GUI.Utils;
using ANZ_AIM_BLL;
using ANZ_AIM_BLL.Model;
using log4net;

namespace ANZ_AIM_GUI
{
    public partial class frmCandidateApproveStatus : Form
    {
        //log4net
        private static readonly ILog logger = LogManager.GetLogger(typeof(frmCandidateApproveStatus));

        protected AppParam appParam = AppParam.Instance;

        public string batchFileID { get; set; }
        public List<SaleInfo> saleInfoList = new List<SaleInfo>();

        public frmCandidateApproveStatus()
        {
            InitializeComponent();
        }

        public frmCandidateApproveStatus(string batchFileID)
            : this()
        {
            try
            {
                this.batchFileID = batchFileID;
            }
            catch (Exception) { throw; }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
            }
        }

        private void btnApprove_Click(object sender, EventArgs e)
        {
            var msg = String.Empty;
            try
            {
                msg = String.Format("{0}, batchFileID: {1}", Constant.SYS_MSG_CANDIDATE_APPROVE, batchFileID);

                //get list candidate approve
                CandidateBLL candidateBll = new CandidateBLL();
                DataTable dtCandidate = candidateBll.GetByBatchFileID(batchFileID);

                //packojbect
                PackObjectData_SaleInfo(dtCandidate);

                //insert into SaleInfo
                SaleInfoBLL saleInfoBll = new SaleInfoBLL();
                saleInfoBll.Save(saleInfoList);

                //get by batchfile to update profile, salecode
                List<SaleInfo> saleInfoListResult = saleInfoBll.GetSaleInfoByBatchFile(batchFileID);
                foreach (SaleInfo si in saleInfoListResult)
                {
                    si.SaleCode = saleInfoBll.GenSaleCode(si.Channel, si.TeamName);
                    si.ProfileID = si.ID.ToString("D6");
                    //update DB: profileid, salecode
                    saleInfoBll.UpdateProfileIDSaleCode(si);
                }

                //approve batch file
                BatchFileUploadBLL batchFileUploadBll = new BatchFileUploadBLL();
                batchFileUploadBll.UpdateStatus(batchFileID, Constant.APPROVED);

                MessageBox.Show("Approve successfully.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
                return;
            }

            try
            {
                if (!String.IsNullOrEmpty(msg))
                {
                    LogBLL logBll = new LogBLL();
                    logBll.Description = msg;
                    logBll.Log(this.FindForm(), appParam.UserName);
                }
            }
            catch { }
        }

        private void PackObjectData_SaleInfo(DataTable dtCandidate)
        {
            try
            {
                saleInfoList.Clear();
                if (dtCandidate != null && dtCandidate.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtCandidate.Rows)
                    {
                        SaleInfo saleInfo = new SaleInfo();
                        saleInfo.CVCode = dr["CVCode"].ToString();
                        saleInfo.SaleCode = "";
                        saleInfo.ProfileID = "";
                        saleInfo.EmployeeFullName = dr["Fullname_V"].ToString();
                        saleInfo.EmployeeFullNameE = dr["Fullname_E"].ToString();
                        saleInfo.Role = dr["Role"].ToString();
                        saleInfo.Level = dr["Level"].ToString();
                        saleInfo.Category = dr["Category"].ToString();
                        saleInfo.ProductCategory = dr["ProductCategory"].ToString();
                        saleInfo.Product = dr["Product"].ToString();
                        saleInfo.TeamName = dr["Team"].ToString();
                        saleInfo.TeamLeaderCode = dr["TeamLeaderCode"].ToString();
                        saleInfo.Channel = dr["Channel"].ToString();
                        saleInfo.JoiningDate = string.IsNullOrEmpty(dr["JoiningDate"].ToString()) ? (DateTime?)null : Convert.ToDateTime(dr["JoiningDate"]);
                        saleInfo.PromotionTransferDate = string.IsNullOrEmpty(dr["PromotionTransferDate"].ToString()) ? (DateTime?)null : Convert.ToDateTime(dr["PromotionTransferDate"]);
                        saleInfo.Status = dr["Status"].ToString();

                        saleInfo.IDCardNumber = dr["IDNumber"].ToString();
                        saleInfo.DateOfBirth = string.IsNullOrEmpty(dr["DateOfBirth"].ToString()) ? (DateTime?)null : Convert.ToDateTime(dr["DateOfBirth"]);
                        saleInfo.IDIssuedDate = string.IsNullOrEmpty(dr["IssuedDate"].ToString()) ? (DateTime?)null : Convert.ToDateTime(dr["IssuedDate"]);
                        saleInfo.IDIssuedPlace = dr["IssuedPlace"].ToString();
                        saleInfo.PermanentAddress = dr["PermanentAddress"].ToString();
                        saleInfo.MobilePhone = dr["CellPhone"].ToString();
                        saleInfo.FingerID = dr["FingerID"].ToString();
                        saleInfo.PersonalTaxCode = dr["PersonalTaxCode"].ToString();

                        saleInfo.ContractType = dr["ContractType"].ToString();
                        saleInfo.ContractTerm = dr["ContractTerm"].ToString();
                        saleInfo.ContractorName = dr["ContractorName"].ToString();
                        saleInfo.ContractSignDate = string.IsNullOrEmpty(dr["ContractSignDate"].ToString()) ? (DateTime?)null : Convert.ToDateTime(dr["ContractSignDate"]);
                        saleInfo.ContractExpiryDate = string.IsNullOrEmpty(dr["ContractExpiryDate"].ToString()) ? (DateTime?)null : Convert.ToDateTime(dr["ContractExpiryDate"]);

                        saleInfo.BankAccount = dr["BankAccount"].ToString();
                        saleInfo.BankBranchAddress = dr["BankBranchAddress"].ToString();
                        saleInfo.BankName = dr["BankName"].ToString();

                        saleInfo.FillingRefCode = dr["FillingRefCode"].ToString();
                        saleInfo.ProfressiveTarget = dr["ProfressiveTarget"].ToString();

                        saleInfo.BatchFileID = batchFileID;
                        saleInfoList.Add(saleInfo);
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }

        private void btnReject_Click(object sender, EventArgs e)
        {
            var msg = String.Empty;
            try
            {
                msg = String.Format("{0}, batchFileID: {1}", Constant.SYS_MSG_CANDIDATE_REJECT, batchFileID);

                BatchFileUploadBLL batchFileUploadBll = new BatchFileUploadBLL();
                //batchFileUploadBll.Delete(batchFileID);  //12Jan2015
                batchFileUploadBll.UpdateStatus(batchFileID, Constant.REJECTED); //12Jan2015 Change status of batch file when removing intead of deleting

                CandidateBLL candidateBll = new CandidateBLL();
                candidateBll.Delete(batchFileID);

                MessageBox.Show("Reject successfully.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
                return;
            }

            try
            {
                if (!String.IsNullOrEmpty(msg))
                {
                    LogBLL logBll = new LogBLL();
                    logBll.Description = msg;
                    logBll.Log(this.FindForm(), appParam.UserName);
                }
            }
            catch { }
        }

        private void btnPending_Click(object sender, EventArgs e)
        {
            var msg = String.Empty;
            try
            {
                msg = String.Format("{0}, batchFileID: {1}", Constant.SYS_MSG_CANDIDATE_PENDING, batchFileID);

                BatchFileUploadBLL batchFileUploadBll = new BatchFileUploadBLL();
                batchFileUploadBll.UpdateStatus(batchFileID, Constant.PENDING);
                MessageBox.Show("Pending successfully.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
                return;
            }

            try
            {
                if (!String.IsNullOrEmpty(msg))
                {
                    LogBLL logBll = new LogBLL();
                    logBll.Description = msg;
                    logBll.Log(this.FindForm(), appParam.UserName);
                }
            }
            catch { }
        }
    }
}
